var e = require("../../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../../@babel/runtime/regenerator")),
  a = require("../../../../@babel/runtime/helpers/asyncToGenerator"),
  t = require("../../../../mycomponent/payKeyboard/PayKeyboardAsync.js"),
  n = getApp();

Page({
  data: {
    language: "",
    transfer_multiple: 0,
    transfer_fetter: 0,
    recharge_value: "",
    recharge_id: "",
    recharge_search: "",
    yz_uid: '', //转账--被转人id
    ratio: 0,
    arrival_count: 0,
    buttonOff: !0,
    love_name: "",
    usable: 0,
    showSearch: !1,
    search_list: [],
    actualVal: 0,
    sell_value: "",
    isclick: !0,
    memberInfo: {
      nickname: "",
      realname: ""
    },
    payKeyboardShow: !1,
    need_password: !1,
    has_password: !1,
    key: "",
    huanhuoNum: '',
    kefuPopShow: false,
    niuzhengPopShow: false
  },
  onLoad: function (e) {
    e.key && this.setData({
      key: e.key
    });
  },
  onReady: function () {
    var e = wx.getStorageSync("langIndex");
    this.setData({
      language: e.en
    });
  },
  onShow: function () {
    this.data.isclick = !0, this.getUsable(), this.getIsHuanhuo();
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  searchTransfer: function (e) {
    var a = this,
      t = e.detail.replace(/\D/g, ""),
      r = "";
    "donate" == this.data.key ? (r = n.getNetAddresss("plugin.love.Frontend.Modules.Member.Controllers.query.index"),
      r += "&recipient=" + t) : (r = n.getNetAddresss("plugin.love.Frontend.Controllers.team-dividend-search.index"),
      r += "&search=" + t), t && (this.setData({
      memberInfo: {
        nickname: "",
        realname: ""
      }
    }), n._getNetWork({
      url: r,
      success: function (e) {
        if (1 === e.data.result)
          if ("donate" == a.data.key) {
            var t = e.data.data.member;
            t && a.setData({
              showSearch: !0,
              "memberInfo.nickname": t.username ? t.username : "",
              "memberInfo.realname": t.realname ? t.realname : ""
            });
          } else {
            var r = e.data.data[0];
            a.setData({
              showSearch: !0,
              "memberInfo.nickname": r.username ? r.username : "",
              "memberInfo.realname": r.nickname ? r.nickname : ""
            });
          }
        else a.setData({
          showSearch: !1
        }), n.tips(e.data.msg);
      },
      fail: function (e) {
        console.log(e);
      }
    }));
  },
  searchTransfer_1: function () {
    var e = this,
      a = this.data.recharge_search;
    if (a) {
      this.setData({
        memberInfo: {
          nickname: "",
          realname: ""
        }
      });
      var t = "";
      "donate" == this.data.key ? (t = n.getNetAddresss("plugin.love.Frontend.Modules.Member.Controllers.query.index"),
        t += "&recipient=" + a) : (t = n.getNetAddresss("plugin.love.Frontend.Controllers.team-dividend-search.index"),
        t += "&search=" + a), n._getNetWork({
        url: t,
        success: function (a) {
          if (1 === a.data.result)
            if ("donate" == e.data.key) {
              var t = a.data.data.member;
              t && e.setData({
                showSearch: !0,
                "memberInfo.nickname": t.username ? t.username : "",
                "memberInfo.realname": t.realname ? t.realname : ""
              });
            } else {
              var r = a.data.data[0];
              e.setData({
                showSearch: !0,
                "memberInfo.nickname": r.username ? r.username : "",
                "memberInfo.realname": r.nickname ? r.nickname : ""
              });
            }
          else e.setData({
            showSearch: !1
          }), n.tips(a.data.msg);
        },
        fail: function (e) {
          console.log(e);
        }
      });
    }
  },
  getIsHuanhuo: function () {
    var e = this,
      a = n.getNetAddresss("HuanNiu.is_huanhuo");
    n._getNetWork({
      url: a,
      data: {
        uid: wx.getStorageSync('uid')
      },
      success: function (a) {
        if (1 === a.data.result) {
          console.log(a.data.data, 'is_huanhuo')
          e.data.huanhuoNum = a.data.data
          e.setData({
            huanhuoNum: a.data.data
          })
        }
      }
    })
  },
  niuKouchu: function () {
    var e = this,
      a = n.getNetAddresss("HuanNiu.huzhuan_kouchu");
    n._getNetWork({
      url: a,
      data: {
        uid: wx.getStorageSync('uid'),
        z_uid: e.data.yz_uid,
        // num: wx.getStorageSync('niu_actualVal'),
        num: wx.getStorageSync('niu_Val')
      },
      success: function (a) {
        if (1 === a.data.result) {
          console.log(a, 'huzhuan_kouchu')
        }
      }
    })
  },
  getUsable: function () {
    var e = this,
      a = n.getNetAddresss("plugin.love.Frontend.Controllers.page.index");
    n._getNetWork({
      url: a,
      success: function (a) {
        if (1 === a.data.result) {
          e.setData({
            usable: a.data.data.usable,
            love_name: a.data.data.love_name,
            ratio: a.data.data.transfer_proportion,
            transfer_multiple: a.data.data.transfer_multiple,
            transfer_fetter: a.data.data.transfer_fetter,
            need_password: a.data.data.transfer_password,
            has_password: a.data.data.has_password,
            point_name: a.data.data.point_name
          }), e.need_sign = a.data.data.need_sign || !1, wx.setNavigationBarTitle({
            title: "donate" == e.data.key ? a.data.data.love_name + e.data.point_name + "转赠" : a.data.data.love_name ? a.data.data.love_name + "转账" : "转账"
          });
          var t = e.data.recharge_value,
            n = e.data.ratio;
          // e.setData({
          //   actualVal: t - t * n / 100
          // });
          e.setData({
            actualVal: t / ((100 - n) / 100)
          })
        } else wx.showToast({
          title: a.data.msg,
          icon: "none"
        });
      },
      fail: function (e) {
        console.log(e);
      }
    });
  },
  payKeyboardComplete: function (e) {
    t.payKeyboardAsync.complete(e.detail);
  },
  getPayKeyboardPassword: function () {
    return this.setData({
      payKeyboardShow: !0
    }), new Promise(function (e, a) {
      t.payKeyboardAsync.addCompleteFn(function (a) {
        e(a);
      });
    });
  },
  payKeyboardClose: function () {
    this.setData({
      payKeyboardShow: !1
    });
  },
  getBalance: function () {
    var t = this;
    return a(e.default.mark(function a() {
      var r, s;
      return e.default.wrap(function (e) {
        for (;;) switch (e.prev = e.next) {
          case 0:
            if (t.data.isclick) {
              e.next = 3;
              break;
            }
            return wx.showToast({
              title: "请勿重复点击",
              icon: "none"
            }), e.abrupt("return");

          case 3:
            if (!t.need_sign) {
              e.next = 6;
              break;
            }
            return wx.showModal({
              title: "提示",
              content: "请先完成合同签署",
              success: function (e) {
                e.confirm && wx.navigateTo({
                  url: "/packageH/sign_shopV2/index/index?scene=3"
                });
              }
            }), e.abrupt("return");

          case 6:
            if (t.data.recharge_search) {
              e.next = 9;
              break;
            }
            return wx.showToast({
              title: "请输入会员ID/手机号/昵称",
              icon: "none"
            }), e.abrupt("return");

          case 9:
            if (t.data.recharge_value) {
              e.next = 12;
              break;
            }
            return wx.showToast({
              title: "请输入您要转账的数额",
              icon: "none"
            }), e.abrupt("return");

          case 12:
            if (t.data.showSearch) {
              e.next = 14;
              break;
            }
            return e.abrupt("return", n.tips("输入的值没有搜索到对应的用户"));

          case 14:
            if (1 != t.data.need_password || 0 != t.data.has_password) {
              e.next = 20;
              break;
            }
            return e.next = 17, n.confirm("请先设置支付密码");

          case 17:
            return e.sent && wx.navigateTo({
              url: "/packageA/member/set_balance_password/set_balance_password"
            }), e.abrupt("return");

          case 20:
            if (r = "", r = "donate" == t.data.key ? n.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.transfer-point.index") : n.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.transfer.index"),
              r += "&recipient=" + t.data.recharge_search, r += "&change_value=" + t.data.actualVal,
              1 != t.data.need_password) {
              e.next = 29;
              break;
            }
            return e.next = 27, t.getPayKeyboardPassword();

          case 27:
            s = e.sent, r += "&password=" + s;

          case 29:
            let newStr = t.data.usable.replace(/,/g, '');
            // t.data.isclick = !1, t.data.actualVal > t.data.huanhuoNum ? (wx.showToast({
            //   title: '换货值不足',
            //   icon: 'none'
            // }), t.data.isclick = !0) : 

            t.data.isclick = !1, t.data.recharge_value > Number(t.data.huanhuoNum) ? (wx.showToast({
              title: '换货值不足',
              icon: 'none'
            }), t.data.isclick = !0) : t.data.actualVal > Number(newStr) ? (wx.showToast({
              title: '牛证数量不足',
              icon: 'none'
            }), t.data.isclick = !0) : t.openNiuPop();
            // }), t.data.isclick = !0) : t.wxPaySxf(r);
          case 31:
          case "end":
            return e.stop();
        }
      }, a);
    }))();
  },
  openNiuPop(){
    this.setData({
      niuzhengPopShow: true
    })
    
  },
  confirmNiuPop(){
    this.setData({
      niuzhengPopShow: false
    })
    this.wxPaySxf();
  },
  cancelNiuPop(){
    this.setData({
      niuzhengPopShow: false,
      isclick: !0
    })
  },
  // wxPaySxf(r) {
  wxPaySxf() {
    
    console.log('wxPaySxf')
    var t = this;
    n._getNetWork({
      url: n.getNetAddresss("HuanNiu.zhuan_pay"),
      data: {
        uid: wx.getStorageSync('uid'),
        jy_uid: t.data.recharge_search,
        // money: t.data.actualVal
        money: t.data.recharge_value
      },
      success: function (res) {
        console.log(res, 'HuanNiu.zhuan_pay')
        let data = res.data
        if (data.result == 1) {
          let payInfo = data.data
          console.log(t.data.actualVal, 't.data.actualVal')
          wx.setStorageSync('niu_actualVal', t.data.actualVal)
          wx.setStorageSync('niu_Val', t.data.recharge_value)
          // 余额支付
          wx.showToast({
            title: '成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(function () {
            t.getUsable(), wx.navigateBack({
              delta: 1
            });
          }, 1e3)
          // 微信支付
          // wx.requestPayment({
          //   timeStamp: payInfo.timeStamp,
          //   nonceStr: payInfo.nonceStr,
          //   package: payInfo.package,
          //   signType: payInfo.signType,
          //   paySign: payInfo.paySign,
          //   success: function (a) {
          //     console.log(a, '成功')
          //     setTimeout(function () {
          //       t.getUsable(), wx.navigateBack({
          //         delta: 1
          //       });
          //     }, 1e3)
          //     //// 微信支付成功不点击完成 不走 --注掉
          //     // n._getNetWork({
          //     //   url: r,
          //     //   success: function (e) {
          //     //     if (1 === e.data.result) {
          //     //       t.data.yz_uid = t.data.recharge_search
          //     //       t.setData({
          //     //         yz_uid: t.data.recharge_search
          //     //       });
          //     //       (wx.showToast({
          //     //         title: e.data.msg,
          //     //         icon: "none"
          //     //       }), t.setData({
          //     //         recharge_value: "",
          //     //         recharge_search: ""
          //     //       }), setTimeout(function () {
          //     //         t.getUsable(), t.niuKouchu(), wx.navigateBack({
          //     //           delta: 1
          //     //         });
          //     //       }, 1e3))
          //     //     }else {
          //     //       t.data.isclick = !0, wx.showToast({
          //     //           title: e.data.msg,
          //     //           icon: "none"
          //     //         })
          //     //     }
          //     //   }
          //     // })
          //   },
          //   fail: function (a) {
          //     console.log(a, '失败');
          //     t.data.isclick = !0
          //     wx.showToast({
          //       title: '支付失败',
          //       icon: "none"
          //     })
          //   }
          // })
        } else {
          t.data.isclick = !0;
          if(data.msg == '用户余额不足'){
            t.setData({
              kefuPopShow: true
            })
          }else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
          
        }
      },
      fail: function (res) {
        t.data.isclick = !0;
        wx.showToast({
          title: '出错了',
          icon: 'none'
        })
      }
    })
  },
   // 联系客服取消按钮
   hideModal(){
    this.setData({
      kefuPopShow: false
    })
  },
  onChange: function (e) {
    this.setData({
      recharge_value: e.detail,
      actualVal: e.detail / ((100 - this.data.ratio) / 100)
    });

  },
  onChange2: function (e) {
    this.setData({
      recharge_search: e.detail.replace(/\D/g, "")
    });
  }
});